/**
 * 商品分类
 * */


var plist = [];


(function () {
})();


//选项
function getOption(v){
    console.log(v);
    $.ajax({
        type: apis.qdGcateOption.method,
        url: apis.qdGcateOption.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data.pid;
            var h = '', i;
            h += '<option value="0">一级</option>';
            for (i = 0; i < d.length; i ++) {
                if (v == d[i].id) {
                    h += '<option value="' + d[i].id + '" selected>';
                } else {
                    h += '<option value="' + d[i].id + '">';
                }
                h += d[i].title;
                h += '</option>';
            }
            $('#pid').html(h);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
function goToGoodsPage(cateId) {

    window.location.href = `/admin/qudao/goods.html?cate_id=${cateId}`;
}

//分类列表
function getList(){
    $.ajax({
        type: apis.qdGcateIndex.method,
        url: apis.qdGcateIndex.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].pname + '</td>';
                h += '<td  style="color: blue"  onclick="goToGoodsPage(' + d[i].id + ')">' + d[i].title + '</td>';
                h += '<td>';
                if (d[i].imgLink == '') {
                    h += '-';
                } else {
                    h += '<img src="' + d[i].imgPrefix + d[i].imgLink + '" height="50"/>';
                }
                h += '</td>';
                h += '<td>' + d[i].uptime + '</td>';
                h += '<td class="f-14">';
                h += '<a href="javascript:;" onclick="gcate_edit(\'编辑\',\'gcate-edit.html\', ' + d[i].id + ');" style="text-decoration:none">';
                h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                h += ' &nbsp; ';
                h += '<a href="javascript:;" onclick="gcate_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
                h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
                h += '</a>';
                h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
                h += '<input type="hidden" id="imgPrefix' + d[i].id + '" value="' + d[i].imgPrefix + '"/>';
                h += '<input type="hidden" id="imgLink' + d[i].id + '" value="' + d[i].imgLink + '"/>';
                h += '<input type="hidden" id="pid' + d[i].id + '" value="' + d[i].pid + '"/>';
                h += '</td>';
                h += '</tr>';
            }
            $('tbody').html(h);
            $('#pageTotal').html(d.length);
            //pages.total = res.total;
            //pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//编辑
function gcate_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var imgPrefix = $('#imgPrefix' + id).val();
    var imgLink = $('#imgLink' + id).val();
    var pid = $('#pid' + id).val();
    tt = tt ? tt : '';
    imgLink = imgLink ? imgLink : '';
    url += '&title=' + escape(tt);
    url += '&pid=' + pid;
    url += '&imgPrefix=' + imgPrefix;
    url += '&imgLink=' + imgLink;
    layer_show(title,url,w,h);
}


//删除
function gcate_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        //$(obj).parents("tr").remove();
        //console.log(id, index);
        $.ajax({
            type: apis.goodsCateDel.method,
            url: apis.goodsCateDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//显示图片
function showImg(e){
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).height(e.h);
}


//保存更新
function modify(){
    var formData = {
        id: id,
        pid: $('#pid').val(),
        title: $('#title').val(),
        img: $('#img').val(),
    };
    if (!formData.title) {
        msgPop({msg: '分类名称必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apis.qdGcateModify.method,
        url: apis.qdGcateModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//导出,被废弃
function daochu(){
    var formData = {};
    $.ajax({
        type: apis.qdGcateOut.method,
        url: apis.qdGcateOut.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            //ms
            $('#down').html('<a href="' + res.data + '" target="_blank" class="btn btn-primary radius">下载数据</a>');
        },
        error: function () {
            console.log('错误辣');
        }
    });
}